package com.sunu.web;

import com.sunu.entity.job.Job;
import com.sunu.entity.worker.Worker;
import com.sunu.entity.worker_job.Worker_Job;
import com.sunu.service.impl.DistributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Create by pengweijie on 2018/7/27
 */
@Controller
@RequestMapping("/distribute")

public class DistributeController {
    @Autowired
    public DistributeService distributeService;

    @RequestMapping(value = "/worker", method = RequestMethod.GET)
    public String workerList(Model model) {
        List<Worker> workerlist = distributeService.getWorkerList();
        model.addAttribute("workerList", workerlist);
        return "WorkerList";
    }

    @RequestMapping(value = "/job", method = RequestMethod.GET)
    public String jobList(Model model) {
        List<Job> jobList = distributeService.getJobList();
        model.addAttribute("jobList", jobList);
        return "jobList";
    }

    @RequestMapping(value = "/workerjob", method = RequestMethod.GET)
    public String workjobList(Model model) {
        List<Worker_Job> worker_jobList = distributeService.getWorkerJobList();
        model.addAttribute("work_jobList", worker_jobList);
        return "WorkerJobList";
    }

    @RequestMapping(value = "/{workerId}/deleteWorker", method = RequestMethod.GET)
    public String deleteWorker(@PathVariable("workerId") Integer workerId, Model model) {
        distributeService.fireWorker(workerId);

        return "redirect:/distribute/worker";
    }

    @RequestMapping(value = "/{jobId}/deleteJob", method = RequestMethod.GET)
    public String deleteJob(@PathVariable("jobId") Integer jobId) {
        distributeService.deleteJob(jobId);

        return "redirect:/distribute/job";
    }

    @RequestMapping(value = "/{workerId}/{jobId}/deleteWorkerJob", method = RequestMethod.GET)
    public String deleteWorkerJob(@PathVariable("workerId") Integer workerId,
                                  @PathVariable("jobId") Integer jobId) {
        distributeService.deleteWorkerJob(workerId, jobId);

        return "redirect:/distribute/workerjob";
    }

    @RequestMapping(value = "/addWorker", method = RequestMethod.GET)
    public String addWorker() {
        return "updataWorker";
    }

    @RequestMapping(value = "/addJob", method = RequestMethod.GET)
    public String addJob() {
        return "updataJob";
    }

    @RequestMapping(value = "/addWorkerJob", method = RequestMethod.GET)
    public String addWorkerJob() {
        return "updataWorkerJob";
    }

    @RequestMapping(value = "/registerworker")
    public String registerworker(@RequestParam("workerId") Integer workerId,
                                 @RequestParam("workerName") String workerName,
                                 @RequestParam("workerSex") String workerSex,
                                 @RequestParam("workerAge") String workerAge,
                                 @RequestParam("workerPhone") String workerPhone) {
        Worker worker = new Worker(workerId, workerName, workerSex, workerAge, workerPhone);
        distributeService.addWorker(worker);
        return "redirect:/distribute/worker";
    }

    @RequestMapping(value = "/registerjob")
    public String registerworker(@RequestParam("jobId") Integer jobId,
                                 @RequestParam("jobName") String jobName,
                                 @RequestParam("salary") Integer salary) {
        Job job = new Job(jobId, jobName, salary);
        distributeService.addJob(job);
        return "redirect:/distribute/job";
    }

    @RequestMapping(value = "distributeJob", method = RequestMethod.POST)
    public String distribute(@RequestParam("workerName") String workerName,
                             @RequestParam("jobName") String jobName) {
        System.out.println(workerName);
        System.out.println(jobName);
        distributeService.DistributeJob(workerName, jobName);
        return "redirect:/distribute/workerjob";

    }

    @RequestMapping(value = "/first", method = RequestMethod.GET)
    public String backHomePage() {
        return "first";
    }


}
